package com.yuncheng.spcyApi.selectXzxkApi.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.yuncheng.spcyApi.mapper.XzXkMapper;
import com.yuncheng.spcyApi.selectXzxkApi.constant.XzXkConstant;
import com.yuncheng.spcyApi.selectXzxkApi.vo.*;
import com.yuncheng.spcyApi.utils.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component(value = "ypltXzxkService")
public class YpltXzxkService {

    private static final Logger log = LoggerFactory.getLogger(YpltXzxkService.class);

    @Resource
    @Lazy
    private XzXkMapper xzXkMapper;

    /**
     *  sqsxbm (001003002) / 药品批发经营企业设立审批
     * @param sqsxid
     * @return
     */
    public Map ypPfJyQySlSp(String sqsxid){
        Map resultMap = new HashMap();

        // 经营范围
        List<XzspJyfwVo> jyfwList = xzXkMapper.selectJyfw(sqsxid);
        if (CollectionUtils.isNotEmpty(jyfwList)){
            resultMap.put("jyfw", jyfwList.get(0));
        }else {
            resultMap.put("jyfw", new JSONObject());
        }

        // 经营场所
        List<XzspJycsVo> jycsList = xzXkMapper.selectJycs(sqsxid);
        if (CollectionUtils.isNotEmpty(jycsList)){
            resultMap.put("jycs", jycsList.get(0));
        }else {
            resultMap.put("jycs", new JSONObject());
        }

        // 计算机配备
        List<XzspJsjpbVo> jsjPbList = xzXkMapper.ypltSelectJsjPb(sqsxid);
        if (CollectionUtils.isNotEmpty(jsjPbList)){
//            JSONObject jsjpbObject = MapUtils.mapToObject(jsjPbList.get(0));
            resultMap.put("jsjpb", jsjPbList.get(0));
        }else {
            resultMap.put("jsjpb", new JSONObject());
        }

        // 设备
        List<XzspSbVo> sbqkList = xzXkMapper.ypltSelectSbQk(sqsxid);
        if (CollectionUtils.isNotEmpty(sbqkList)){
            resultMap.put("sbqk", sbqkList.get(0));
        }else {
            resultMap.put("sbqk", new JSONObject());
        }

        // 药品运输车辆、运输设备表
        List<XzspYssbVo> yssbList = xzXkMapper.ypltSelectYssb(sqsxid);
//        JSONArray yssbListArr = MapUtils.listMapToJson(yssbList);
        resultMap.put("yssbList", yssbList);

        // 仓库信息
        List<XzspCkVo> ckList = xzXkMapper.ypltSelectCkxx(sqsxid);
//        JSONArray ckListArr = MapUtils.listMapToJson(ckList);
        resultMap.put("ckList", ckList);

        // 人员数量
        List<XzspRyslVo> ryslList = xzXkMapper.selectRysl(sqsxid);
        if (CollectionUtils.isNotEmpty(ryslList)){
            resultMap.put("rysl", ryslList.get(0));
        }else {
            resultMap.put("rysl", new JSONObject());
        }

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);

        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 企业人员-主要人员 / 其他人员
        resultMap.putAll(this.qyryListMap(sqsxid));

        return resultMap;
    }

    /**
     * sqsxbm (001003004) / 药品经营批发企业药品经营许可证换发审批
     */
    public Map ypJypfQyYpJyXkzHfSp(String sqsxid){
        Map resultMap = new HashMap();

        // 变更前、变更后的 内容;
        resultMap.put("lsxx", this.lsxx(sqsxid));

        // 计算机配备
        List<XzspJsjpbVo> jsjPbList = xzXkMapper.ypltSelectJsjPb(sqsxid);
        if (CollectionUtils.isNotEmpty(jsjPbList)){
//            JSONObject jsjpbObject = MapUtils.mapToObject(jsjPbList.get(0));
            resultMap.put("jsjpb", jsjPbList.get(0));
        }else {
            resultMap.put("jsjpb", new JSONObject());
        }

        // 设备
        List<XzspSbVo> sbqkList = xzXkMapper.ypltSelectSbQk(sqsxid);
        if (CollectionUtils.isNotEmpty(sbqkList)){
            resultMap.put("sbqk", sbqkList.get(0));
        }else {
            resultMap.put("sbqk", new JSONObject());
        }

        // 药品运输车辆、运输设备表
        List<XzspYssbVo> yssbList = xzXkMapper.ypltSelectYssb(sqsxid);
        resultMap.put("yssbList", yssbList);

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 企业其他人员
        List<QyRyVo> qtQyryList = xzXkMapper.selectQyRyList(sqsxid, XzXkConstant.Fzrlx_Type_00, false);
        resultMap.put("qtQyryList", qtQyryList);

        // 企业人员数量
        List<XzspRyslVo> ryslList = xzXkMapper.selectRysl(sqsxid);
        if (CollectionUtils.isNotEmpty(ryslList)){
            resultMap.put("rysl", ryslList.get(0));
        }else {
            resultMap.put("rysl", new JSONObject());
        }

        // 经营场所
        List<XzspJycsVo> jycsList = xzXkMapper.selectJycs(sqsxid);
        if (CollectionUtils.isNotEmpty(jycsList)){
            resultMap.put("jycs", jycsList.get(0));
        }else {
            resultMap.put("jycs", new JSONObject());
        }

        // 经营范围
        List<XzspJyfwVo> jyfwList = xzXkMapper.selectJyfw(sqsxid);
        if (CollectionUtils.isNotEmpty(jyfwList)){
            resultMap.put("jyfw", jyfwList.get(0));
        }else {
            resultMap.put("jyfw", new JSONObject());
        }

        return resultMap;
    }

    /**
     * sqsxbm (001003006) / 零售连锁总部药品经营许可证核发
     */
    public Map lsLsZbYpJyXkzHf(String sqsxid){
        Map resultMap = new HashMap();

        // 经营范围
        List<XzspJyfwVo> jyfwList = xzXkMapper.selectJyfw(sqsxid);
        if (CollectionUtils.isNotEmpty(jyfwList)){
            resultMap.put("jyfw", jyfwList.get(0));
        }else {
            resultMap.put("jyfw", new JSONObject());
        }

        // 经营场所
        List<XzspJycsVo> jycsList = xzXkMapper.selectJycs(sqsxid);
        if (CollectionUtils.isNotEmpty(jycsList)){
            resultMap.put("jycs", jycsList.get(0));
        }else {
            resultMap.put("jycs", new JSONObject());
        }

        // 计算机配备
        List<XzspJsjpbVo> jsjPbList = xzXkMapper.ypltSelectJsjPb(sqsxid);
        if (CollectionUtils.isNotEmpty(jsjPbList)){
//            JSONObject jsjpbObject = MapUtils.mapToObject(jsjPbList.get(0));
            resultMap.put("jsjpb", jsjPbList.get(0));
        }else {
            resultMap.put("jsjpb", new JSONObject());
        }

        // 设备
        List<XzspSbVo> sbqkList = xzXkMapper.ypltSelectSbQk(sqsxid);
        if (CollectionUtils.isNotEmpty(sbqkList)){
            resultMap.put("sbqk", sbqkList.get(0));
        }else {
            resultMap.put("sbqk", new JSONObject());
        }

        // 药品运输车辆、运输设备表
        List<XzspYssbVo> yssbList = xzXkMapper.ypltSelectYssb(sqsxid);
        resultMap.put("yssbList", yssbList);

        // 仓库信息
        List<XzspCkVo> ckList = xzXkMapper.ypltSelectCkxx(sqsxid);
//        JSONArray ckListArr = MapUtils.listMapToJson(ckList);
        resultMap.put("ckList", ckList);

        // 人员数量
        List<XzspRyslVo> ryslList = xzXkMapper.selectRysl(sqsxid);
        if (CollectionUtils.isNotEmpty(ryslList)){
            resultMap.put("rysl", ryslList.get(0));
        }else {
            resultMap.put("rysl", new JSONObject());
        }

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);

        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 企业人员-主要人员 / 其他人员
        resultMap.putAll(this.qyryListMap(sqsxid));

        return resultMap;
    }


    /**
     * sqsxbm (001003003) / 药品经营批发企业《药品经营许可证》事项变更审批
     */
    public Map ypjyPfqyYpjyXkzSxbgSp(String sqsxid){
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 变更前、变更后的 内容;
        resultMap.put("lsxx", this.lsxx(sqsxid));

        // 经营范围
        List<XzspJyfwVo> jyfwList = xzXkMapper.selectJyfw(sqsxid);
        if (CollectionUtils.isNotEmpty(jyfwList)){
            resultMap.put("jyfw", jyfwList.get(0));
        }else {
            resultMap.put("jyfw", new JSONObject());
        }

        return resultMap;
    }

    /**
     * sqsxbm (001003039) / 药品批发企业药品现代物流企业确认件发放
     */
    public Map ypPfqyYpXdwlqyQrff(String sqsxid){
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 经营范围
        List<XzspJyfwVo> jyfwList = xzXkMapper.selectJyfw(sqsxid);
        if (CollectionUtils.isNotEmpty(jyfwList)){
            resultMap.put("jyfw", jyfwList.get(0));
        }else {
            resultMap.put("jyfw", new JSONObject());
        }

        return resultMap;
    }

    /**
     * sqsxbm (001003019) / 药品批发企业经营蛋白同化制剂、肽类激素审批
     */
    public Map yppfqyjytljssp(String sqsxid){
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 企业人员-主要人员
        List<QyRyVo> qyryList = xzXkMapper.selectQyRyList(sqsxid, null, true);
        resultMap.put("primaryQyryList", qyryList);

        // 企业人员数量
        List<XzspRyslVo> ryslList = xzXkMapper.selectRysl(sqsxid);
        if (CollectionUtils.isNotEmpty(ryslList)){
            resultMap.put("rysl", ryslList.get(0));
        }else {
            resultMap.put("rysl", new JSONObject());
        }

        // 药品生产企业经营场所
        List<XzspJycsVo> jycsList = xzXkMapper.selectJycs(sqsxid);
        if (CollectionUtils.isNotEmpty(jycsList)){
            resultMap.put("jycs", jycsList.get(0));
        }else {
            resultMap.put("jycs", new JSONObject());
        }

        // 经营范围
        List<XzspJyfwVo> jyfwList = xzXkMapper.selectJyfw(sqsxid);
        if (CollectionUtils.isNotEmpty(jyfwList)){
            resultMap.put("jyfw", jyfwList.get(0));
        }else {
            resultMap.put("jyfw", new JSONObject());
        }

        // 设备
        List<XzspSbVo> sbqkList = xzXkMapper.ypltSelectSbQk(sqsxid);
        resultMap.put("sbList", sbqkList);

        return resultMap;
    }

    /**
     * sqsxbm (001003012) / 麻醉药品和第一类精神药品区域性批发企业经营审批 (待确认)(未开发)
     */

    /**
     * sqsxbm (001003030) / 药品零售连锁企业从事第二类精神药品零售业务审批 (待确认)(未开发)
     */

    /**
     * sqsxbm (001003058) / 专门从事第二类精神药品批发企业经营审批
     */
    public Map zmcsDelJsypPfqyjySp(String sqsxid){
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 药品批发经营资格
        List<XzspYppfjyzgVo> yppfjyzgList = xzXkMapper.ypltYppfjyzg(sqsxid);
        if (CollectionUtils.isNotEmpty(yppfjyzgList)){
            resultMap.put("jyzg", yppfjyzgList.get(0));
        }else {
            resultMap.put("jyzg", new JSONObject());
        }

        return resultMap;
    }

    /**
     * sqsxbm (001003026) / 药品经营零售企业药品经营许可证换发审批
     */
    public Map ypjyLsqyYpjyXkzhfSp(String sqsxid){
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 企业其他人员
        List<QyRyVo> qtQyryList = xzXkMapper.selectQyRyList(sqsxid, XzXkConstant.Fzrlx_Type_00, false);
        resultMap.put("qtQyryList", qtQyryList);

        // 企业人员数量
        List<XzspRyslVo> ryslList = xzXkMapper.selectRysl(sqsxid);
        if (CollectionUtils.isNotEmpty(ryslList)){
            resultMap.put("rysl", ryslList.get(0));
        }else {
            resultMap.put("rysl", new JSONObject());
        }

        // 药品生产企业经营场所
        List<XzspJycsVo> jycsList = xzXkMapper.selectJycs(sqsxid);
        if (CollectionUtils.isNotEmpty(jycsList)){
            resultMap.put("jycs", jycsList.get(0));
        }else {
            resultMap.put("jycs", new JSONObject());
        }

        // 经营范围
        List<XzspJyfwVo> jyfwList = xzXkMapper.selectJyfw(sqsxid);
        if (CollectionUtils.isNotEmpty(jyfwList)){
            resultMap.put("jyfw", jyfwList.get(0));
        }else {
            resultMap.put("jyfw", new JSONObject());
        }

        // 计算机配备
        List<XzspJsjpbVo> jsjPbList = xzXkMapper.ypltSelectJsjPb(sqsxid);
        resultMap.put("jsjPbList", jsjPbList);

        // 设备
        List<XzspSbVo> sbqkList = xzXkMapper.ypltSelectSbQk(sqsxid);
        resultMap.put("sbList", sbqkList);

        // 药品运输车辆、运输设备表
        List<XzspYssbVo> yssbList = xzXkMapper.ypltSelectYssb(sqsxid);
        resultMap.put("yssbList", yssbList);

        // 变更前、变更后的 内容;
        resultMap.put("lsxx" ,this.lsxx(sqsxid));

        return resultMap;
    }

    /**
     * sqsxbm (001003008) / 零售连锁总部药品经营许可证换发
     */
    public Map lslszbYpjyXkzHf(String sqsxid){
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 企业其他人员
        List<QyRyVo> qtQyryList = xzXkMapper.selectQyRyList(sqsxid, XzXkConstant.Fzrlx_Type_00, false);
        resultMap.put("qtQyryList", qtQyryList);

        // 企业人员数量
        List<XzspRyslVo> ryslList = xzXkMapper.selectRysl(sqsxid);
        if (CollectionUtils.isNotEmpty(ryslList)){
            resultMap.put("rysl", ryslList.get(0));
        }else {
            resultMap.put("rysl", new JSONObject());
        }

        // 药品生产企业经营场所
        List<XzspJycsVo> jycsList = xzXkMapper.selectJycs(sqsxid);
        if (CollectionUtils.isNotEmpty(jycsList)){
            resultMap.put("jycs", jycsList.get(0));
        }else {
            resultMap.put("jycs", new JSONObject());
        }

        // 经营范围
        List<XzspJyfwVo> jyfwList = xzXkMapper.selectJyfw(sqsxid);
        if (CollectionUtils.isNotEmpty(jyfwList)){
            resultMap.put("jyfw", jyfwList.get(0));
        }else {
            resultMap.put("jyfw", new JSONObject());
        }

        // 计算机配备
        List<XzspJsjpbVo> jsjPbList = xzXkMapper.ypltSelectJsjPb(sqsxid);
        resultMap.put("jsjPbList", jsjPbList);

        // 设备
        List<XzspSbVo> sbqkList = xzXkMapper.ypltSelectSbQk(sqsxid);
        resultMap.put("sbList", sbqkList);

        // 药品运输车辆、运输设备表
        List<XzspYssbVo> yssbList = xzXkMapper.ypltSelectYssb(sqsxid);
        resultMap.put("yssbList", yssbList);

        // 变更前、变更后的 内容;
        resultMap.put("lsxx" ,this.lsxx(sqsxid));

        return resultMap;
    }

    /**
     * sqsxbm (001003023) / 放射性药品经营许可证核发 (待确认)(未开发)
     */

    /**
     * sqsxbm (001003047) / 放射性药品经营许可证延续 (待确认)(未开发)
     */

    /**
     * sqsxbm (001003025) / 药品经营（零售企业）设立验收审批
     */
    public Map ypjyLsqySlysSp(String sqsxid){
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 企业人员- 主要人员- 其他人员
        resultMap.putAll(this.qyryListMap(sqsxid));

        // 企业人员数量
        List<XzspRyslVo> ryslList = xzXkMapper.selectRysl(sqsxid);
        if (CollectionUtils.isNotEmpty(ryslList)){
            resultMap.put("rysl", ryslList.get(0));
        }else {
            resultMap.put("rysl", new JSONObject());
        }

        // 仓库信息
        List<XzspCkVo> ckList = xzXkMapper.ypltSelectCkxx(sqsxid);
//        JSONArray ckListArr = MapUtils.listMapToJson(ckList);
        resultMap.put("ckList", ckList);

        // 药品生产企业经营场所
        List<XzspJycsVo> jycsList = xzXkMapper.selectJycs(sqsxid);
        if (CollectionUtils.isNotEmpty(jycsList)){
            resultMap.put("jycs", jycsList.get(0));
        }else {
            resultMap.put("jycs", new JSONObject());
        }

        // 经营范围
        List<XzspJyfwVo> jyfwList = xzXkMapper.selectJyfw(sqsxid);
        if (CollectionUtils.isNotEmpty(jyfwList)){
            resultMap.put("jyfw", jyfwList.get(0));
        }else {
            resultMap.put("jyfw", new JSONObject());
        }

        // 计算机配备
        List<XzspJsjpbVo> jsjPbList = xzXkMapper.ypltSelectJsjPb(sqsxid);
        if (CollectionUtils.isNotEmpty(jsjPbList)){
            resultMap.put("jsjpb", jsjPbList.get(0));
        }else {
            resultMap.put("jsjpb", new JSONObject());
        }

        // 设备
        List<XzspSbVo> sbqkList = xzXkMapper.ypltSelectSbQk(sqsxid);
        if (CollectionUtils.isNotEmpty(sbqkList)){
            resultMap.put("sb", sbqkList.get(0));
        }else {
            resultMap.put("sb", new JSONObject());
        }

        // 药品运输车辆、运输设备表
        List<XzspYssbVo> yssbList = xzXkMapper.ypltSelectYssb(sqsxid);
        if (CollectionUtils.isNotEmpty(yssbList)){
            resultMap.put("yssb", yssbList.get(0));
        }else {
            resultMap.put("yssb", new JSONObject());
        }

        return resultMap;
    }

    /**
     * sqsxbm (001003010) / 购买第一类中的药品类易制毒化学品审批
     */
    public Map gmDylzdYplYzdhxpSp(String sqsxid){
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 生产第一类中的药品类易制毒化学品审批
        List<XzspScdylzdyplyzdhxpVo> yzdhxpList = xzXkMapper.scDylzdYplYzdhxpSp(sqsxid);
        if (CollectionUtils.isNotEmpty(yzdhxpList)){
            resultMap.put("yzdhxp", yzdhxpList.get(0));
        }else {
            resultMap.put("yzdhxp", new JSONObject());
        }

        return resultMap;
    }

    /**
     * 企业人员-主要人员 / 其他人员
     */
    public Map qyryListMap(String sqsxid){
        Map resultMap = new HashMap();

        // 企业人员-主要人员
        List<QyRyVo> qyryList = xzXkMapper.selectQyRyList(sqsxid, null, true);
        resultMap.put("primaryQyryList", qyryList);

        // 企业其他人员
        List<QyRyVo> qtQyryList = xzXkMapper.selectQyRyList(sqsxid, XzXkConstant.Fzrlx_Type_00, false);
        resultMap.put("qtQyryList", qtQyryList);

        return resultMap;
    }

    /**
     * 查询 - 历史表 - 历史记录 - 返回 -变更前，变更后的信息
     */
    public JSONObject lsxx(String sqsxid){
        String ccdx = "";
        Map<String, Object> lsbMap = xzXkMapper.searchLsb(sqsxid);
        if (lsbMap != null){
            Map<String, Object> lsxxMap = xzXkMapper.searchLsjl(lsbMap.get("ID").toString());
            if (lsxxMap != null){
                try {
                    ccdx =  MapUtils.ClobToString((Clob)lsxxMap.get("CCDX"));
                } catch (SQLException e) {
                    log.error("SQLException[]: {}", e.getMessage());
                } catch (IOException e) {
                    log.error("SQLException[]: {}", e.getMessage());
                }
            }
        }

        if (StringUtils.isNotBlank(ccdx)){
            JSONObject jsonObject = JSONObject.parseObject(ccdx);
            return jsonObject;
        }else {
            return new JSONObject();
        }
    }

}
